package a.student.dao;

import java.util.ArrayList;

import a.student.library.*;
import a.student.library.Class;
import a.student.helper.DatabaseHelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ContactDAO extends BaseDao<Contact>{

	public ContactDAO(Context context, String databaseName) {
		super(context, databaseName);
	}
	
	public ContactDAO(SQLiteDatabase db) {
		super(db);
	}
	
	public ContactDAO(DatabaseHelper helper) {
		super(helper);
	}
	
	public long addNewContact(Contact contactInfo){

		ContentValues cv=new ContentValues();
		cv.put(Contact.CONTACT_NAME, contactInfo.getName());
		cv.put(Contact.CONTACT_EMAIL, contactInfo.getEmail());
		cv.put(Contact.CONTACT_PHONENO, contactInfo.getPhoneNo());
		cv.put(Contact.CONTACT_TYPE, contactInfo.type);
		cv.put(Contact.CONTACT_WEBSITE, contactInfo.website);
		cv.put(Contact.CONTACT_NOTE, contactInfo.note);
		cv.put(Contact.CONTACT_ADDRESS, contactInfo.address);
		cv.put(Contact.CONTACT_PHOTO, contactInfo.photo);
		
		
		long result = super.insert(Contact.CONTACT_TBLNAME, cv);
		if(result > 0)
			contactInfo.setContactId((int)result);
		return result;
		
	}

	public long removeContact(Contact contactInfo){
		ContentValues cv = new ContentValues();
		cv.put(Contact.CONTACT_ID, contactInfo.getContactId());
		long count = super.delete(Contact.CONTACT_TBLNAME, cv);
		return count;
		
	}
    
    public ArrayList<Contact> getAllContact(){
    	Cursor c = super.getAll(Contact.CONTACT_TBLNAME, Contact.CONTACT_DEFAULT_SORT_ORDER);
    	ArrayList<Contact> array = new ArrayList<Contact>();
    	if(c==null)
    		return array;
    	while(!c.isAfterLast()){
    		array.add(new Contact(c));
    		c.moveToNext();
    	}
    	c.close();
    	return array;
    }
    
    
    
    public Contact findContact(final int contactID) {
        ContentValues cv = new ContentValues();
        cv.put(Contact.CONTACT_ID, contactID);
        Cursor c = super.find(Class.CLASS_TBLNAME, cv,Class.CLASS_DEFAULT_SORT_ORDER);
        Contact contact = null;
        if (c != null) {
            try {
            	contact = new Contact(c);
            } catch (Exception e) {
            	e.printStackTrace();
			}
            finally {
                c.close();
            }
        }
        return contact;
    }
    
	public boolean updateContact(Contact contactInfo){
		
		boolean flag = true;
		ContentValues cv=new ContentValues();
		cv.put(Contact.CONTACT_NAME, contactInfo.getName());
		cv.put(Contact.CONTACT_EMAIL, contactInfo.getEmail());
		cv.put(Contact.CONTACT_PHONENO, contactInfo.getPhoneNo());
		cv.put(Contact.CONTACT_WEBSITE, contactInfo.getWebsite());
		cv.put(Contact.CONTACT_NOTE, contactInfo.getNote());
		cv.put(Contact.CONTACT_ADDRESS, contactInfo.getAddress());
		flag = super.update(Contact.CONTACT_TBLNAME, cv, Contact.CONTACT_ID + "=?", new String[]{String.valueOf(contactInfo.getContactId())})>0;
    	return flag;
	}


}
